﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace NutriLeaf.Phone.Pages.Sync
{
    public partial class Sync : PhoneApplicationPage
    {
        public Sync()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            SyncNow();
        }

        private void SyncNow()
        {
            NutriLeaf.Phone.Sync.NutriLeafSync.UserSyncCompleted += new EventHandler(NutriLeafSync_UserSyncCompleted);
            NutriLeaf.Phone.Sync.NutriLeafSync.SyncUser();

        }

        void NutriLeafSync_UserSyncCompleted(object sender, EventArgs e)
        {
            NutriLeaf.Phone.Sync.NutriLeafSync.TipSyncCompleted += new EventHandler(NutriLeafSync_TipSyncCompleted);
            NutriLeaf.Phone.Sync.NutriLeafSync.SyncTips();
        }

        void NutriLeafSync_TipSyncCompleted(object sender, EventArgs e)
        {
            NutriLeaf.Phone.Sync.NutriLeafSync.ArticleSyncCompleted += new EventHandler(NutriLeafSync_ArticleSyncCompleted);
            NutriLeaf.Phone.Sync.NutriLeafSync.SyncArticles();
            
        }

        void NutriLeafSync_ArticleSyncCompleted(object sender, EventArgs e)
        {
            NavigateToDashboard();
        }

        private void NavigateToDashboard()
        {
            Dispatcher.BeginInvoke(delegate
            {
                NavigationService.Navigate(new Uri(Constants.Navigation.Dashboard,UriKind.RelativeOrAbsolute));
            });
            
        }



    }
}